<html>
<head><meta charset="utf-8"><title>Running C tests for ffi crate · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html">Running C tests for ffi crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213259097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213259097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213259097">(Oct 14 2020 at 08:28)</a>:</h4>
<p>Whats the correct way to make <code>cargo test</code> compile+run the C tests for ffi crates?</p>
<ol>
<li>how do you optionally build the C test code in <a href="http://build.rs">build.rs</a></li>
<li>what exactly should execute those tests with <code>cargo test</code>, should I add a <code>#[test]</code> that calls <code>std::process::Command</code> on those test files?</li>
</ol>



<a name="213273926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213273926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213273926">(Oct 14 2020 at 11:19)</a>:</h4>
<p>There isn’t a standard way of writing tests in C, so I think you’d have to describe that to start with.</p>



<a name="213278103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213278103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213278103">(Oct 14 2020 at 12:04)</a>:</h4>
<p>You're right, "usually" there's some build system stuff and then you get somesort of <code>test</code> binary that you execute</p>



<a name="213279483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213279483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213279483">(Oct 14 2020 at 12:17)</a>:</h4>
<p>Does the cc crate even offer a way to produce executables?</p>



<a name="213279596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213279596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213279596">(Oct 14 2020 at 12:18)</a>:</h4>
<p>Why do you want to rerun tests that presumably are run by whoever created the C library?</p>



<a name="213279678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213279678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213279678">(Oct 14 2020 at 12:19)</a>:</h4>
<p>How do you run the Rust tests for a Rust dependency as part of your build?</p>



<a name="213280226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213280226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213280226">(Oct 14 2020 at 12:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213279483">said</a>:</p>
<blockquote>
<p>Does the cc crate even offer a way to produce executables?</p>
</blockquote>
<p>I don't know.</p>



<a name="213280268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213280268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213280268">(Oct 14 2020 at 12:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213279596">said</a>:</p>
<blockquote>
<p>Why do you want to rerun tests that presumably are run by whoever created the C library?</p>
</blockquote>
<p>Because some sensitive libraries you'd want to run the tests on the target machine</p>



<a name="213280379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213280379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213280379">(Oct 14 2020 at 12:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213279678">said</a>:</p>
<blockquote>
<p>How do you run the Rust tests for a Rust dependency as part of your build?</p>
</blockquote>
<p>That's a great question, Ideally the end executable will vendor all the deps and run their tests? (just like <code>make check</code> usually runs all the deps <code>make check</code>'s)</p>
<p>otherwise, what if a user uses an arch/flags we never tested before?</p>



<a name="213280883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213280883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213280883">(Oct 14 2020 at 12:30)</a>:</h4>
<p>I think there are <em>two</em> possible different things going on: a <code>-sys</code>-like crate that wants to make sure the C library it depends on works correctly, and thus wants to run its test suite. Imagining some magic invocation <em>à la</em> <code>make test</code>, then an integration test that ran <code>Command::new("make")...</code> would be, imho, the way to go.</p>
<p>The other option is, for an "FFI crate", to actually be providing some functionality for a C / FFI downstream user, by exporting FFI functions. In that case, since the <code>cargo</code> interaction with such <code>FFI</code> crate is mainly to build the C library, I don't think it would make sense to have the C / FFI downstream user tests of your implementation be run within <code>cargo test</code>. The "sensible" thing would be to have an outside command (<em>e.g.</em>, a script, a <code>make</code> / <code>just</code> / <code>cargo xtask</code> invocation) that would <code>cargo build</code> your crate, put the generated library file in the right place, and then run the C / FFI downstream user with it.</p>



<a name="213282061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213282061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213282061">(Oct 14 2020 at 12:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213280268">said</a>:</p>
<blockquote>
<p>Because some sensitive libraries you'd want to run the tests on the target machine</p>
</blockquote>
<p>Ah, so this is code that will only ever be run on the machine it is compiled on and never copied to another computer. That's a pretty small subset of programs, so I can see why this isn't a trivial thing to achieve.</p>



<a name="213282358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213282358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213282358">(Oct 14 2020 at 12:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213280379">said</a>:</p>
<blockquote>
<p>just like <code>make check</code> usually runs all the deps <code>make check</code>'s</p>
</blockquote>
<p>That's news to me! I didn't think there <em>was</em> a "usual" when it came to C build systems. That sounds incredibly painful /time-consuming as every new layer will re-run the tests from the previous layer.</p>



<a name="213282505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213282505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213282505">(Oct 14 2020 at 12:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213280379">said</a>:</p>
<blockquote>
<p>otherwise, what if a user uses an arch/flags we never tested before?</p>
</blockquote>
<p>I'd spend my time running the tests in their "natural habitat" instead. Check out the C library and build and test it, completely distinct from Rust / Cargo.</p>



<a name="213282700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213282700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213282700">(Oct 14 2020 at 12:48)</a>:</h4>
<p>Perhaps you need to follow the pattern of the <a href="https://crates.io/crates/openssl-src">openssl-src</a> crate, which is a whole crate which only exists to be able to build openssl.</p>



<a name="213282744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213282744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213282744">(Oct 14 2020 at 12:48)</a>:</h4>
<p>It's like a build script extracted into it's own crate, effectively.</p>



<a name="213283400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213283400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213283400">(Oct 14 2020 at 12:54)</a>:</h4>
<p>Then you could have your main project build the library and a separate project build and run the executable</p>



<a name="213283438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213283438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213283438">(Oct 14 2020 at 12:54)</a>:</h4>
<p>Last I looked into it, a build script doesn't know things like "release vs debug" or "build vs check vs test"</p>



<a name="213291496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Running%20C%20tests%20for%20ffi%20crate/near/213291496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate.html#213291496">(Oct 14 2020 at 13:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Running.20C.20tests.20for.20ffi.20crate/near/213282358">said</a>:</p>
<blockquote>
<p>That's news to me! I didn't think there <em>was</em> a "usual" when it came to C build systems. That sounds incredibly painful /time-consuming as every new layer will re-run the tests from the previous layer.</p>
</blockquote>
<p>Obviously there's no "standard" in C build systems, but this is what I've personally seen in a lot of C/C++ systems either that I work on, or OSS stuff(although other people might have different experience)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>